<?php

class SendEmail {
    /*
     * DTOAN
     * Send mail book
     * id =1 : register success
     */

    public static function sendEmailBooksuccess($model, $event, $main_event, $book) {

        $event_send_email = Event::model()->with('tickets','translate')->findByPk($main_event->id);
        $seat_map = Ticket::model()->findByAttributes(array('event_id' => $main_event->id,'seat_map' => 1));
        $booked = Booked::model()->with('booked_details')->findByPk($book);

        $aBody = array(
            '{EMAIL}' => $booked->email_contact,
            '{TICKET_ORDER_N}' => $booked->ticket_order_number,
            '{FULL_NAME}' => $model->first_name . ' ' . $model->last_name,
            '{VN_EVENT_TITLE}' => $event_send_email->translate[0]['main_title'],
            '{VN_EVENT_SUB_TITLE}' => $event_send_email->translate[0]['sub_title'],
            '{VN_LOCATION}' => $event_send_email->translate[0]['location'],
            '{VN_ADDRESS}' => $event_send_email->translate[0]['address'],
            '{EN_EVENT_TITLE}' => $event_send_email->translate[1]['main_title'],
            '{EN_EVENT_SUB_TITLE}' => $event_send_email->translate[1]['sub_title'],
            '{EN_LOCATION}' => $event_send_email->translate[1]['location'],
            '{EN_ADDRESS}' => $event_send_email->translate[1]['address'],
            '{TIME}' => date_format(new DateTime($main_event['start_time']), 'h:i A') . ',' . date_format(new DateTime($main_event['start_date']), 'd - m , Y')
        );
        $html_vi = '<style>
                        table{
                            width:100%;
                        }
                        th, td
                        {
                            padding-left:10px;
                            padding-right:10px;
                        }
                        table, td, th
                        {
                            border-collapse:collapse;
                            border:1px solid green;
                            text-align:center;
                        }
                        th
                        {
                            background-color:green;
                            color:white;
                        }
                    </style>
                    <table border="1" cellpadding="10" style="border-collapse: collapse;text-align: center;">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Loại Vé</th>
                                <th>Số Lượng</th>
                                <th>Giá Vé</th>
                                <th>Tổng Giá Vé</th>
                                <th>Chỗ Ngồi</th>
                            </tr>
                        </thead>';

        $htmlTmp = '
                        <tbody>
                            <tr>
                                <td>colum0</td>
                                <td>colum1</td>
                                <td>colum2</td>
                                <td>colum3</td>
                                <td>colum4</td>
                                <td>colum5</td>
                            </tr>
                        </tbody>
        ';
        $html_en = '<table  border="1" cellpadding="10" style="border-collapse: collapse;text-align: center;">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Ticket Type</th>
                                <th>Quantity</th>
                                <th>Price</th> 
                                <th>Total Price</th>
                                <th>Seat Position</th>
                            </tr>
                        </thead>';
        foreach ($booked->booked_details as $k => $ticket){
            // Get booked details information
            $ticket_details = BookedDetail::model()->with('ticket')->findByPk($ticket->id);
            if($ticket_details->seat != null){
                $seat = CJSON::decode($ticket_details->seat);
                $seat_html = null;
                foreach ($seat as $value) {
                    $seat_html .= $value . " ";
                }
                $html_vi .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',$seat_html), $htmlTmp);
                $html_en .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',$seat_html), $htmlTmp);  
            }
            else{
                $html_vi .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',' Chưa có'), $htmlTmp);
                $html_en .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',' Not Available'), $htmlTmp); 
            }
            
        }
        $html_vi .='</table>';
        $html_en .='</table>';
        if ($seat_map != null) {
            $aBody['{SEAT_MAP}'] = '<img width="560" src="http://' .$_SERVER['SERVER_NAME'] . '/data/pic/' .$seat_map->pic_seat_map .'" />' ;
        }else{
            $aBody['{SEAT_MAP}'] = null;
        }
        $aBody['{VN_TICKET}'] = $html_vi;
        $aBody['{EN_TICKET}'] = $html_en;

        $aSubject = array(
            '{TICKET_ORDER_N}' => $booked->ticket_order_number,
            '{VN_EVENT_TITLE}' => $event_send_email->translate[0]['main_title'],
            '{EN_EVENT_TITLE}' => $event_send_email->translate[1]['main_title'],
        );

        $setting = Yii::app()->setting;
//        if(CmsEmail::sendmail(1, $aSubject, $aBody, $mUser->email,$setting->getItem('smtpUsername'))){
        if (CmsEmail::sendmail(1, $aSubject, $aBody, $model->email, $setting->getItem('smtpUsername'),$aBody['{SEAT_MAP}']) && CmsEmail::sendmail(1, $aSubject, $aBody, $booked->email_contact, $setting->getItem('smtpUsername'),$aBody['{SEAT_MAP}'])) {
            
        } else {
            $mUser->addError('email', 'Can not send email');
        }
    }
    public static function sendMailSuccess($book){
        $event_send_email = Event::model()->with('tickets','translate')->findByPk($book->event_id);
        $user = Users::model()->findByPk($book->user_id);
        if (isset($book->percentage)) {
            $price_percent = MyFunctionCustom::percentage_calculate($book->total_price,$book->percentage);
            $total_price = MyFunctionCustom::percentage_detail($book->total_price,$book->percentage);
        }
        
        $aBody = array(
            '{EMAIL}' => $book->email_contact,
            '{TICKET_ORDER_N}' => $book->ticket_order_number,
            '{TOTAL_COST}' => number_format($book->total_price) . ' VNĐ',
            '{PERCENT}' => (isset($book->percentage) ? $book->percentage : '0 %'),
            '{PRICE_OF_PERCENT}' => (isset($price_percent) ? number_format($price_percent) . ' VNĐ' : ' 0 VNĐ'),
            '{PRICE_AFTER_PERCENT}' => (isset($total_price) ? number_format($total_price) . ' VNĐ' : number_format($book->total_price) . ' VNĐ'),
            '{FULL_NAME}' => $user->first_name . ' ' . $user->last_name,
            '{VN_EVENT_TITLE}' => $event_send_email->translate[0]['main_title'],
            '{VN_EVENT_SUB_TITLE}' => $event_send_email->translate[0]['sub_title'],
            '{VN_LOCATION}' => $event_send_email->translate[0]['location'],
            '{VN_ADDRESS}' => $event_send_email->translate[0]['address'],
            '{EN_EVENT_TITLE}' => $event_send_email->translate[1]['main_title'],
            '{EN_EVENT_SUB_TITLE}' => $event_send_email->translate[1]['sub_title'],
            '{EN_LOCATION}' => $event_send_email->translate[1]['location'],
            '{EN_ADDRESS}' => $event_send_email->translate[1]['address'],
            '{TIME}' => date_format(new DateTime($event_send_email['start_time']), 'h:i A') . ',' . date_format(new DateTime($event_send_email['start_date']), 'd - m , Y'),
            '{PAYMENT_LINK_VI}' =>'<p><a href="'.Pay123::getPayMent($book).'">Quý khách vui lòng click vào link này để thanh toán</a>',
            '{PAYMENT_LINK_EN}' =>'<p><a href="'.Pay123::getPayMent($book).'">Please click this link to process your payment</a>',
        );
        $html_vi='';
         if($book->payment_type==2 || $book->payment_type ==3){
              
                }
        $html_vi .= '<style>
                        table{
                            width:100%;
                        }
                        th, td
                        {
                            padding-left:10px;
                            padding-right:10px;
                        }
                        table, td, th
                        {
                            border-collapse:collapse;
                            border:1px solid green;
                            text-align:center;
                        }
                        th
                        {
                            background-color:green;
                            color:white;
                        }
                    </style>
                    <table border="1" cellpadding="10" style="border-collapse: collapse;text-align: center;">
                        <thead>
                            <tr>
                                <th></th>
                                <th>Loại Vé</th>
                                <th>Số Lượng</th>
                                <th>Giá Vé</th>
                                <th>Tổng Giá Vé</th>
                                <th>Chỗ Ngồi</th>
                            </tr>
                        </thead>';

        $htmlTmp = '
                        <tbody>
                            <tr>
                                <td>colum0</td>
                                <td>colum1</td>
                                <td>colum2</td>
                                <td>colum3</td>
                                <td>colum4</td>
                                <td>colum5</td>
                            </tr>
                        </tbody>
        ';
        $html_en = '<table border="1" cellpadding="10" style="border-collapse: collapse;text-align:center">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Ticket Type</th>
                                <th>Quantity</th>
                                <th>Price</th> 
                                <th>Total Price</th>
                                <th>Seat Position</th>
                            </tr>
                        </thead>';
        foreach ($book->booked_details as $k => $ticket){
            // Get booked details information
            $ticket_details = BookedDetail::model()->with('ticket')->findByPk($ticket->id);
            if($ticket_details->seat != null){
                $seat = CJSON::decode($ticket_details->seat);
                $seat_html = null;
                foreach ($seat as $value) {
                    $seat_html .= $value . " ";
                }
                $html_vi .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',$seat_html), $htmlTmp);
                $html_en .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',$seat_html), $htmlTmp);  
            }
            else{
                $html_vi .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',' Chưa có'), $htmlTmp);
                $html_en .= str_replace(array('colum0','colum1', 'colum2', 'colum3', 'colum4','colum5'), array($k + 1,$ticket_details->ticket->description_of_ticket, $ticket->quantity, number_format($ticket_details->ticket->price) . ' VNĐ', number_format($ticket_details->total_price) . ' VNĐ',' Not Available'), $htmlTmp); 
            }
            
        }
         
        $html_vi .='</table>';
        $html_en .='</table>';
        
        $aBody['{VN_TICKET}'] = $html_vi;
        $aBody['{EN_TICKET}'] = $html_en;
        $setting = Yii::app()->setting;
        $aSubject = array(
            '{TICKET_ORDER_N}' => $book->ticket_order_number,
            '{VN_EVENT_TITLE}' => $event_send_email->translate[0]['main_title'],
            '{EN_EVENT_TITLE}' => $event_send_email->translate[1]['main_title'],
        );
//        if(CmsEmail::sendmail(1, $aSubject, $aBody, $mUser->email,$setting->getItem('smtpUsername'))){
        if (CmsEmail::sendmail(2, $aSubject, $aBody, $user->email, $setting->getItem('smtpUsername1')) && CmsEmail::sendmail(2, $aSubject, $aBody, $book->email_contact, $setting->getItem('smtpUsername1')) ) {
            
        } else {
            $mUser->addError('email', 'Can not send email');
        }
    }
    
    public static function sendMailBarcode($book,$tickets){
        $event_send_email = Event::model()->with('tickets','translate')->findByPk($book->event_id);
        $user = Users::model()->findByPk($book->user_id);
        if (isset($book->percentage)) {
            $price_percent = MyFunctionCustom::percentage_calculate($book->total_price,$book->percentage);
            $total_price = MyFunctionCustom::percentage_detail($book->total_price,$book->percentage);
        }
        
        
        $html_vi='';
        $html_en='';
         foreach($tickets as $ticket)
         {
            $html_vi .= '
                <div style="border:1px solid #ddd;border-radius:5px;"><table style="width:600px;margin:0 auto" >
                    <tr><td style="text-align:center" colspan=2>
                <img src="'.Yii::app()->getBaseUrl(true).'/barcode.php?code='.$ticket->barcode_hash.'">
                    <br/>
                    '.$ticket->barcode_hash.'
                  </td></tr>'
                    . '<tr><td style="text-align:left;padding:20px;color:black">'
                    . '<b style="color:#1474b4">'.$event_send_email->translate[0]['main_title'].'</b>
                        <br/><span style="color:#fc8825">'.$event_send_email->translate[0]['sub_title'].'</span><br/>
                        <br/>'.date_format(new DateTime($event_send_email['start_time']), 'h:i A') . ',' . date_format(new DateTime($event_send_email['start_date']), 'd - m , Y').'
                        <br/>'.$event_send_email->translate[0]['location'].'
                        <br/>Loại vé: '.$ticket->detail->ticket->description_of_ticket.'
                        <br/>Giá tiền: '. number_format($ticket->detail->total_price) . ' VNĐ'.'

                        </td>
                        <td style="text-align:left;padding:20px">
                        <b style="color:#1474b4">'.$event_send_email->translate[1]['main_title'].'</b>
                        <br/><span style="color:#fc8825">'.$event_send_email->translate[1]['sub_title'].'</span><br/>
                        <br/>'.date_format(new DateTime($event_send_email['start_time']), 'h:i A') . ',' . date_format(new DateTime($event_send_email['start_date']), 'd - m , Y').'
                        <br/>'.$event_send_email->translate[1]['location'].'
                        <br/>Type: '.$ticket->detail->ticket->description_of_ticket.'
                        <br/>Price: '. number_format($ticket->detail->total_price) . ' VNĐ'.'

                        </td></tr>
                        </table>
                        </div>
                ';
         }
        $body = $html_vi;
        $setting = Yii::app()->setting;
        $aSubject='Ticket for check in';
//        if(CmsEmail::sendmail(1, $aSubject, $aBody, $mUser->email,$setting->getItem('smtpUsername'))){
        if (CmsEmail::sendbarcode($aSubject, $body, $user->email, $setting->getItem('smtpUsername1'))) {
            
        } else {
            $mUser->addError('email', 'Can not send email');
        }
        if (CmsEmail::sendbarcode($aSubject, $body, $book->email_contact, $setting->getItem('smtpUsername1'))) {
            
        } else {
            $mUser->addError('email', 'Can not send email');
        }
    }

}

?>
